/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ContentManagers.Models;

import ContentManagers.Security.Authenticator;
import DBConnection.ConnectionFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import ContentManagers.Security.Validator;
import java.sql.ResultSet;

/**
 *
 * @author Martin San Diego
 */
public class TransactionManager {
    
    public boolean newTransaction(int customer_id)
    {   boolean result = false;
    
        try{
            //opens db connection
            ConnectionFactory myFactory = ConnectionFactory.getFactory("customer");
            Connection conn = myFactory.getConnection();
    
            //SQL Query
            PreparedStatement pstmt = conn.prepareStatement("INSERT INTO transactions (customer_id, transaction_id, transaction_timestamp) VALUES(?, NULL, CURRENT_TIMESTAMP)");
            
            pstmt.setString(1, Integer.toString(customer_id));
            
            //execute query
            pstmt.executeUpdate();

            //close DB connection
            conn.close();

            result=true;
            }
            catch(SQLException e)
            {
               e.printStackTrace();
            }
        return result;
    }
    
    public int getLastTransactionID(String customer_id)
    {   int lastTransaction = -111;
        
    try {
            ConnectionFactory myFactory = ConnectionFactory.getFactory("customer");
            Connection conn = myFactory.getConnection();
          
            int i = 1;

            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM transactions where customer_id = ? ORDER BY transaction_timestamp desc LIMIT 1");
            pstmt.setString(i++, customer_id);
            
            ResultSet rs = pstmt.executeQuery();

            while (rs.next()) {
                lastTransaction = rs.getInt("transaction_id");
            }
            
            conn.close();
         }  catch (SQLException ex) { ex.printStackTrace();}
            
        return lastTransaction;
    }

    public boolean addTransactionItem(String transaction_id, String quantity, String product_id, String purchase_value)
    {
        boolean result = false;
    
        try{
            //opens db connection
            ConnectionFactory myFactory = ConnectionFactory.getFactory("customer");
            Connection conn = myFactory.getConnection();
    
            //SQL Query
            PreparedStatement pstmt = conn.prepareStatement("INSERT INTO items_per_transaction (transaction_id, quantity, product_id, purchase_value) VALUES(?, ?, ?, ?)");
            
            int i = 1;
            pstmt.setString(i++, transaction_id);
            pstmt.setString(i++, quantity);
            pstmt.setString(i++, product_id);
            pstmt.setString(i++, purchase_value);
            
            //execute query
            pstmt.executeUpdate();

            //close DB connection
            conn.close();

            result=true;
            }
            catch(SQLException e)
            {
               e.printStackTrace();
            }
        return result;
    }
}
